﻿using Bottle.CommunityForum.Factory;
using Bottle.CommunityForum.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Bottle.CommunityForum.BLL
{
    public abstract class BaseService<T> where T : class,new()
    {
        public IDbSession Db = DbSessionFactory.GetCurrentDbSession();

        public IBaseDal<T> CurrentDal { get; set; }

        public BaseService()
        {
            GetCurrentDal();
        }

        public abstract void GetCurrentDal();

        #region 查询

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="wherelambda"></param>
        /// <returns></returns>
        public IQueryable<T> QueryEntitys(Expression<Func<T, bool>> wherelambda)
        {
            return CurrentDal.QueryEntitys(wherelambda);
        }

        /// <summary>
        /// 分页查询(order by)
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="totle"></param>
        /// <param name="wherelambda"></param>
        /// <param name="ordlambda"></param>
        /// <returns></returns>
        public IQueryable<T> QueryPageOrdEntitys<S>(int pageSize, int pageIndex, out int totle, Expression<Func<T, bool>> wherelambda, Expression<Func<T, S>> ordlambda)
        {
            return CurrentDal.QueryPageOrdEntitys(pageSize, pageIndex, out totle, wherelambda, ordlambda);
        }

        /// <summary>
        /// 分页查询(order by desc)
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="totle"></param>
        /// <param name="wherelambda"></param>
        /// <param name="ordlambda"></param>
        /// <returns></returns>
        public IQueryable<T> QueryPageDescEntitys<S>(int pageSize, int pageIndex, out int totle, Expression<Func<T, bool>> wherelambda, Expression<Func<T, S>> ordlambda)
        {
            return CurrentDal.QueryPageDescEntitys(pageSize, pageIndex, out totle, wherelambda, ordlambda);
        }
        #endregion

        #region 添加

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T AddEntity(T entity)
        {
            CurrentDal.AddEntity(entity);
            Db.SaveChanges();
            return entity;
        }
        #endregion

        #region 修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool EditEntity(T entity)
        {
            CurrentDal.EditEntity(entity);
            return Db.SaveChanges() > 0;
        }
        #endregion

        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DelEntity(T entity)
        {
            CurrentDal.DelEntity(entity);
            return Db.SaveChanges() > 0;
        }
        #endregion

        public int SaveChanges()
        {
            return Db.SaveChanges();
        }
    }
}
